package com.example.config;

import java.nio.charset.Charset;
import java.util.Arrays;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.example.config.filter.HttpRequestFilter;

@Configuration
@EnableWebMvc
@EnableRedisRepositories
public class MvcConfig extends WebMvcConfigurerAdapter {
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("login").setViewName("login");
		registry.addViewController("/").setViewName("index");
	}
	
	@Bean
	public HttpMessageConverter<String> responseBodyConverter() {
	    StringHttpMessageConverter converter = new StringHttpMessageConverter();
	    converter.setSupportedMediaTypes(Arrays.asList(
	    		new MediaType("text", "plain", Charset.forName("UTF-8")),
	    		new MediaType("text", "html", Charset.forName("UTF-8")),
	    		new MediaType("application", "json", Charset.forName("UTF-8"))
	    		));
	    return converter;
	}
	
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
	
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
	
	@Bean
	public FilterRegistrationBean myOncePerRequestFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new HttpRequestFilter() {
			@Override
			public boolean exclusive(String uri) {
				if (uri.startsWith("/static")) {
					return true;
				}
				return false;
			}
		});
		registration.addUrlPatterns("/*");// 拦截路径
		registration.setName("HttpRequestFilter");// 拦截器名称
		registration.setOrder(Ordered.HIGHEST_PRECEDENCE);// 顺序
		return registration;
	}
	
	/*@Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HttpRequestDumperFilter() {
			@Override
			public boolean exclusive(String uri) {
				if (uri.startsWith("/static")) {
					return true;
				}
				return false;
			}
		}).addPathPatterns("/**");

    }
	*/
	/*@Bean
	public HttpRequestDumperFilter requestDumperFilter() {
        return new HttpRequestDumperFilter() {
			@Override
			public boolean exclusive(String uri) {
				if (uri.startsWith("/static")) {
					return true;
				}
				return false;
			}
		};
    }*/
	/*@Bean
	public RequestDumperFilter requestDumperFilter() {
        return new RequestDumperFilter();
    }*/
}